<template>
	<view class="address-form p24">
		<form @submit="formSubmit" @reset="formReset">
			<view class="bg-white p-0-30 f30">
				<view class="d-s-c border-b-d9">
					<text class="key-name">收货人</text>
					<input class="ml20 flex-1 f32 p-30-0" name="name" type="text" placeholder-class="grary9"
						v-model="address.name" placeholder="请输入收货人姓名" />
				</view>
				<view class="d-s-c border-b-d9">
					<text class="key-name">联系方式</text>
					<input class="ml20 flex-1 f32 p-30-0" name="phone" type="text" placeholder-class="grary9"
						v-model="address.phone" placeholder="请输入收货人手机号" />
				</view>
				<view class="d-s-c border-b-d9" @click="showMulLinkageThreePicker">
					<text class="key-name">所在地区</text>
					<view class="input-box flex-1">
						<input class="ml20 f32 flex-1 p-30-0" type="text" placeholder-class="grary9" placeholder=""
							v-model="selectCity" disabled="true" style="pointer-events: none" />
					</view>
				</view>
				<view class="d-s-c border-b-d9">
					<text class="key-name">详细地址</text>
					<textarea class="ml20 flex-1 p-30-0 lh150" name="detail" :auto-height="true"
						v-model="address.detail" placeholder-class="grary9" placeholder="请输入街道小区楼牌号等"></textarea>
				</view>
			</view>

			<view class="mt24"><button form-type="submit" class="add-btn theme-btn">保存</button>
			</view>
		</form>
		<mpvue-city-picker v-if="is_load" ref="mpvueCityPicker" :province="province" :city="city" :area="area"
			:pickerValueDefault="cityPickerValueDefault" @onConfirm="onConfirm"></mpvue-city-picker>
	</view>
</template>

<script>
	// import mpvueCityPicker from "@/components/mpvue-citypicker/mpvueCityPicker.vue";
	import mpvueCityPicker from '@/components/mpvue-citypicker/mpvueCityPicker.vue';
	export default {
		components: {
			mpvueCityPicker,
		},
		data() {
			return {
				cityPickerValueDefault: [0, 0, 0],
				selectCity: "选择省,市,区",
				provinceId: 0,
				cityId: 0,
				regionId: 0,
				address: {},
				delta: 1,
				province: [],
				city: [],
				area: [],
				is_load: false,
			};
		},
		onLoad: function(options) {
			this.delta = options.delta;
			this.getData();
		},
		methods: {
			// 获取省市区
			getData() {
				let self = this;
				this.$Request.post('/api/front/settings/getRegion').then(res => {
					// self._post("settings/getRegion", {}, function (res) {
					self.province = res.data[0];
					self.city = res.data[1];
					self.area = res.data[2];
					self.is_load = true;
				});
			},
			/*提交*/
			formSubmit: function(e) {
				let self = this;
				var formdata = e.detail.value;
				formdata.provinceId = self.provinceId;
				formdata.cityId = self.cityId;
				formdata.regionId = self.regionId;
				console.log(formdata);
				if (formdata.name == "") {
					uni.showToast({
						title: "请输入收货人姓名",
						duration: 1000,
						icon: "none",
					});
					return false;
				}

				if (formdata.phone == "") {
					uni.showToast({
						title: "请输入手机号码",
						duration: 1000,
						icon: "none",
					});
					return false;
				}

				if (!/^1(3|4|5|6|7|8|9)\d{9}$/.test(formdata.phone)) {
					uni.showToast({
						title: "手机有误,请重填！",
						duration: 2000,
						icon: "none",
					});
					return false;
				}

				if (
					formdata.provinceId == 0 ||
					formdata.cityId == 0 ||
					formdata.regionId == 0
				) {
					uni.showToast({
						title: "请选择完整省市区",
						duration: 1000,
						icon: "none",
					});
					return false;
				}

				if (formdata.detail == "") {
					uni.showToast({
						title: "请输入街道小区楼牌号等",
						duration: 1000,
						icon: "none",
					});
					return false;
				}
				// self._postBody("user/address/add", formdata, function (res) {
				this.$Request.postJson('/api/front/user/address/add', formdata).then(res => {
					self.showSuccess(res.msg, function() {
						// #ifndef H5
						uni.navigateBack({
							delta: parseInt(self.delta),
						});
						// #endif
						// #ifdef H5
						history.go(-self.delta);
						// #endif
					});
				});
			},

			formReset: function(e) {
				console.log("清空数据");
			},

			/*三级联动选择*/
			showMulLinkageThreePicker() {
				this.$refs.mpvueCityPicker.show();
			},

			/*确定选择的省市区*/
			onConfirm(e) {
				this.selectCity = e.label;
				this.provinceId = e.cityCode[0];
				this.cityId = e.cityCode[1];
				this.regionId = e.cityCode[2];
			},
		},
	};
</script>

<style lang="scss">
	.address-list {
		margin-top: 20rpx;
		border-radius: 20rpx;
	}

	.address-form .key-name {
		width: 240rpx;
		font-size: 28rpx;
	}

	.ml20.flex-1.f32.p-30-0 {
		height: 50px;
	}

	.add-btn {
		position: fixed;
		left: 24rpx;
		bottom: calc(env(safe-area-inset-bottom) + 20rpx);
		margin: 20rpx auto;
		box-sizing: border-box;
		display: flex;
		justify-content: center;
		align-items: center;
		width: 692rpx;
		height: 88rpx;
		border-radius: 88rpx;
		line-height: 88rpx;
		text-align: center;
		font-size: 30rpx;
		font-family: PingFang SC;
		font-weight: 800;
	}
</style>